package com.jtpay.unionpay.product.http.retrofit;

import android.content.Context;
import android.net.ParseException;
import android.text.TextUtils;
import com.google.gson.JsonParseException;
import com.jtpay.unionpay.product.R;
import com.jtpay.unionpay.product.http.dataStructure.BasicResponse;
import com.jtpay.unionpay.product.utils.LogUtil;
import com.jtpay.unionpay.product.utils.ProgressDialogUtil;
import com.jtpay.unionpay.product.utils.manager.ToastUtil;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import org.json.JSONException;
import retrofit2.HttpException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import static com.jtpay.unionpay.product.http.retrofit.RetrofitObserver.ExceptionReason.*;

public class RetrofitObserver<T extends BasicResponse> implements Observer<T> {
    private ProgressDialogUtil mDialogUtils;

    protected RetrofitObserver(Context context) {
        this(context, true);
    }

    private RetrofitObserver(Context context, boolean isShowLoading) {
        if (isShowLoading) {
            mDialogUtils = new ProgressDialogUtil();
            mDialogUtils.showProgress(context, context.getResources().getString(R.string.loading));
        }
    }

    @Override
    public void onSubscribe(Disposable d) {
        LogUtil.log("onSubscribe = " + d.toString());

    }

    @Override
    public void onNext(T response) {
        dismissProgress();
        if (response.isFlag()) {
            onSuccess(response);
        } else {
            onFail(response);
        }
    }

    /**
     * 请求数据成功 且响应码为200
     *
     * @param response 服务器返回的数据
     */
    public void onSuccess(T response) {

    }

    /**
     * 服务器返回数据，但响应码不为200
     *
     * @param response 服务器返回的数据
     */
    public void onFail(T response) {
        LogUtil.log("onFail = " + response.toString());

        String message = response.getMsg();
        if (!TextUtils.isEmpty(message)) {
            ToastUtil.showShort(message);
        }
//        ToastUtil.showShort(R.string.response_return_error);
    }


    private void dismissProgress() {
        if (mDialogUtils != null) {
            mDialogUtils.dismissProgress();
        }
    }

    @Override
    public void onError(Throwable e) {
        LogUtil.log("RetrofitObserver onError " + e.getMessage());
        dismissProgress();
        if (e instanceof HttpException) {     //   HTTP错误
            onException(BAD_NETWORK);
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException) {   //   连接错误
            onException(CONNECT_ERROR);
        } else if (e instanceof InterruptedIOException) {   //  连接超时
            onException(CONNECT_TIMEOUT);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {   //  解析错误
            onException(PARSE_ERROR);
        } else {
            onException(UNKNOWN_ERROR);
        }
    }

    /**
     * 请求异常
     *
     * @param reason ExceptionReason
     */
    private void onException(ExceptionReason reason) {
        switch (reason) {
            case CONNECT_ERROR:
                ToastUtil.showShort(R.string.connect_error);
                break;

            case CONNECT_TIMEOUT:
                ToastUtil.showShort(R.string.connect_timeout);
                break;

            case BAD_NETWORK:
                ToastUtil.showShort(R.string.bad_network);
                break;

            case PARSE_ERROR:
                ToastUtil.showShort(R.string.parse_error);
                break;

            case UNKNOWN_ERROR:
            default:
                ToastUtil.showShort(R.string.unknown_error);
                break;
        }
    }

    @Override
    public void onComplete() {
        dismissProgress();
    }

    public enum ExceptionReason {
        CONNECT_ERROR, CONNECT_TIMEOUT, BAD_NETWORK, PARSE_ERROR, UNKNOWN_ERROR
    }
}
